<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<md-dialog aria-label="Add member" layout-padding flex="30">
	<md-dialog-content class="searchMembers">
		<form name="formMember" ng-submit="$event.preventDefault()">
			<md-autocomplete
					md-selected-item="selectedItem"
					md-search-text="searchText"
					md-selected-item-change="selectUser(user)"
					md-items="user in searchUser(searchText)"
					md-item-text="user.reference"
					md-min-length="1"
          md-delay="500"
					placeholder="Search user..."
					md-autofocus="true"
          md-menu-class="autocomplete-search-user"
					flex>
				<md-item-template>
          <md-list-item class="md-2-line contact-item">
            <gravitee-identity-picture 
              image-id="user.id"
              image-url="getUserAvatar(user.id)"
              image-name="user.displayName"
              image-width="40"
              image-border-radius="true"
              class="md-avatar"
            ></gravitee-identity-picture>
            <div class="md-list-item-text compact">
              <h3>{{user.displayName}}</h3>
              <span ng-if="user.email">{{user.email}}</span>
            </div>
          </md-list-item>
				</md-item-template>
				<md-not-found>
					No user matching "{{searchText}}" were found, or users already selected.
				</md-not-found>
			</md-autocomplete>

      <br />

      <md-list class="fixedRows" ng-show="usersSelected.length > 0">
        <md-subheader class="md-no-sticky">Selected users</md-subheader>
        <md-list-item class="md-2-line contact-item" ng-repeat="(index, user) in usersSelected">
          <gravitee-identity-picture 
            image-id="user.id"
            image-url="getUserAvatar(user.id)"
            image-name="user.displayName"
            image-width="40"
            image-border-radius="true"
            class="md-avatar"
            ></gravitee-identity-picture>
            
          <div class="md-list-item-text compact">
            <h3>{{user.displayName}}</h3>
          </div>
        </md-list-item>
      </md-list>

      <div ng-show="usersSelected.length > 0">
        <md-subheader class="md-no-sticky">Select role</md-subheader>
        <md-input-container class="md-block">
          <label>Role</label>
          <md-select ng-model="role" required>
            <md-option ng-repeat="role in roles" ng-value="role" ng-disabled="role.system">
              {{role.name}}
            </md-option>
          </md-select>
        </md-input-container>
      </div>
    </form>
	</md-dialog-content>

	<md-dialog-actions layout="row">
		<md-button ng-click="hide()">
			Cancel
		</md-button>
		<md-button type="submit" class="md-raised md-primary" ng-click="addMembers()" ng-disabled="formMember.$invalid || usersSelected.length === 0">
			Add
		</md-button>
	</md-dialog-actions>
</md-dialog>
